//
//  AYSHomePageViewController.m
//  AiYingShiProject
//
//  Created by lanou on 15/10/27.
//  Copyright (c) 2015年 李晓理. All rights reserved.
//

#import "AYSHomePageViewController.h"
#import "AYSHomeTableViewCell.h"
#import "AYSRequestManager.h"
#import "AYSHomePageModel.h"
#import "AYSHomeMoreDataViewController.h"
#import "AYSHomeMoreModel.h"
#import "AYSHomeSearchBarTableVC.h"
#import "AYSHomeSearchBarModel.h"
#import "AYSCommonPlayViewController.h"
#import "SVProgressHUD.h"
#import "AYSSideViewController.h"
#import "AppDelegate.h"
@interface AYSHomePageViewController ()<UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate>
@property(nonatomic, strong)UITableView *mainTableView;// 首页主界面tableView
@property(nonatomic, strong)UIView *topView;
// items的数据源
@property(nonatomic ,strong)NSMutableArray *movieArr;
@property(nonatomic ,strong)NSMutableArray *TVArr;
@property(nonatomic ,strong)NSMutableArray *zyArr;
@property(nonatomic ,strong)NSMutableArray *dmArr;
@property(nonatomic, assign)BOOL flag1;
@property(nonatomic, assign)BOOL flag2;
@property(nonatomic, assign)BOOL flag3;
@property(nonatomic, assign)BOOL flag4;
// 更多数据源
@property(nonatomic ,strong)NSMutableArray *moreMovie;
@property(nonatomic ,strong)NSMutableArray *moreTV;
@property(nonatomic ,strong)NSMutableArray *moreZY;
@property(nonatomic ,strong)NSMutableArray *moreDM;

@property(nonatomic, strong)UIButton *homeButton;
@end

@implementation AYSHomePageViewController
#pragma wark ==================在请求数据同时有值的时候再创建界面
- (void)setFlag1:(BOOL)flag1
{
    _flag1 = flag1;
    if (_flag1 && _flag2 && _flag3 && _flag4)
    {
        [self getMainTableView];
    }
}
- (void)setFlag2:(BOOL)flag2
{
    _flag2 = flag2;
    if (_flag1 && _flag2 && _flag3 && _flag4)
    {
        [self getMainTableView];
    }

}

- (void)setFlag3:(BOOL)flag3
{
    _flag3 = flag3;
    if (_flag1 && _flag2 && _flag3 && _flag4)
    {
        [self getMainTableView];
    }

}
- (void)setFlag4:(BOOL)flag4
{
    _flag4 = flag4;
    if (_flag1 && _flag2 && _flag3 && _flag4)
    {
        [self getMainTableView];
    }

}

#pragma wark 首页界面网络请求
- (void)requestDataWithHomePage
{
#pragma wark ========================电影
    [AYSRequestManager requestWithType:GET url:KHomeForMovie parDic:nil finish:^(NSData *data) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        NSDictionary *dict = dic[@"result"][0];
        NSArray *listArr = dict[@"list"];
        for (NSDictionary *listDic in listArr)
        {
            AYSHomePageModel *model = [[AYSHomePageModel alloc] init];
            [model setValuesForKeysWithDictionary:listDic];
            [_movieArr addObject:model];
           
        }
        self.flag1 = YES;
        [_mainTableView reloadData];
    } error:^(NSError *error)
     {
         NSLog(@"电影网络请求失败");
    }];
    
#pragma wark ==========================电视剧
    [AYSRequestManager requestWithType:GET url:KHomeForTV parDic:nil finish:^(NSData *data) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        NSDictionary *dict = dic[@"result"][0];
        NSArray *listArr = dict[@"list"];
        for (NSDictionary *listDic in listArr)
        {
            AYSHomePageModel *model = [[AYSHomePageModel alloc] init];
            [model setValuesForKeysWithDictionary:listDic];
            [_TVArr addObject:model];
        }
        
        self.flag2 = YES;
        [_mainTableView reloadData];
    } error:^(NSError *error)
     {
         NSLog(@"电视剧网络请求失败");
     }];

#pragma wark ===============================综艺
    [AYSRequestManager requestWithType:GET url:KHomeForZY parDic:nil finish:^(NSData *data) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        NSDictionary *dict = dic[@"result"][0];
        NSArray *listArr = dict[@"list"];
        for (NSDictionary *listDic in listArr)
        {
            AYSHomePageModel *model = [[AYSHomePageModel alloc] init];
            [model setValuesForKeysWithDictionary:listDic];
            [_zyArr addObject:model];
        }
         self.flag3 = YES;
        [_mainTableView reloadData];
    } error:^(NSError *error)
     {
         NSLog(@"综艺网络请求失败");
     }];

#pragma wark =============================动漫
    [AYSRequestManager requestWithType:GET url:KHomeForDM parDic:nil finish:^(NSData *data) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        NSDictionary *dict = dic[@"result"][0];
        NSArray *listArr = dict[@"list"];
        for (NSDictionary *listDic in listArr)
        {
            AYSHomePageModel *model = [[AYSHomePageModel alloc] init];
            [model setValuesForKeysWithDictionary:listDic];
            [_dmArr addObject:model];
        }
        self.flag4 = YES;
        [_mainTableView reloadData];
    } error:^(NSError *error)
     {
         NSLog(@"动漫网络请求失败");
     }];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];
    self.navigationController.navigationBarHidden = YES;

    
    // 数组初始化
    _movieArr = [NSMutableArray arrayWithCapacity:0];
    _TVArr = [NSMutableArray arrayWithCapacity:0];
    _zyArr = [NSMutableArray arrayWithCapacity:0];
    _dmArr = [NSMutableArray arrayWithCapacity:0];
    _moreDM = [NSMutableArray arrayWithCapacity:0];
    _moreMovie = [NSMutableArray arrayWithCapacity:0];
    _moreTV = [NSMutableArray arrayWithCapacity:0];
    _moreZY = [NSMutableArray arrayWithCapacity:0];
    [SVProgressHUD show];
    // 请求数据
    [self requestDataWithHomePage];
    [SVProgressHUD dismiss];
    [self topView];
    

}

#pragma wark ====================创建topView
- (UIView *)topView
{
    if (!_topView)
    {
        _topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 65)];
        _topView.backgroundColor = [UIColor colorWithWhite:0.066 alpha:1.000];
        
        _homeButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
         _homeButton.frame = CGRectMake(5, 25, 35, 35);
        [_homeButton setImage:[UIImage imageNamed:@"抽屉菜单"] forState:(UIControlStateNormal)];
        [_homeButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        [_homeButton addTarget:self action:@selector(backToHomeMainVC:) forControlEvents:(UIControlEventTouchUpInside)];
        
        UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 2 - 10, 25, 50, 30)];
        nameLabel.textColor = [UIColor whiteColor];
        nameLabel.text = @"首页";
        nameLabel.font = [UIFont systemFontOfSize:17];
        
        [_topView addSubview:nameLabel];
        [self.topView addSubview:_homeButton];
        [self.view addSubview:_topView];
    }
    return _topView;
}

- (void)backToHomeMainVC:(UIButton *)button
{
    AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
    AYSSideViewController *sideViewController = [delegate sideViewController];
    [sideViewController showLeftViewController:true];
}
#pragma wark ===================创建tableView
- (void)getMainTableView
{
    _mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 71, SCREEN_WIDTH, SCREEN_HEIGHT - 120)];
    _mainTableView.delegate = self;
    _mainTableView.dataSource = self;
    _mainTableView.rowHeight = 280;
    _mainTableView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:_mainTableView];
    
    // 注册
    [_mainTableView registerClass:[AYSHomeTableViewCell class] forCellReuseIdentifier:@"reuse"];
}

#pragma wark ======================配置数据源
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AYSHomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor blackColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    switch (indexPath.row)
    {
        case 0:
            cell.HomeDataArr = _movieArr;
            cell.categoryLable.text = @"电影";
            cell.categoryImage.image = [UIImage imageNamed:@"电影"];
            break;
        case 1:
            cell.HomeDataArr = _TVArr;
            cell.categoryLable.text = @"电视剧";
            cell.categoryImage.image = [UIImage imageNamed:@"电视剧"];
            break;
        case 2:
            cell.HomeDataArr = _zyArr;
            cell.categoryLable.text = @"综艺";
            cell.categoryImage.image = [UIImage imageNamed:@"综艺"];
            break;
        case 3:
            cell.HomeDataArr = _dmArr;
            cell.categoryLable.text = @"动漫";
            cell.categoryImage.image = [UIImage imageNamed:@"动漫"];
            break;
            
        default:
            break;
    }
    cell.navigationController = self.navigationController;
    [cell.moreButton addTarget:self action:@selector(loginHomeMoreDataVC:) forControlEvents:(UIControlEventTouchUpInside)];
    cell.moreButton.tag = 1000 + indexPath.row;
    [cell.collectionView reloadData];
    return cell;
}


#pragma wark ==========================进入更多界面
- (void)loginHomeMoreDataVC:(UIButton *)button
{
    AYSHomeMoreDataViewController *moreDataVC = [[AYSHomeMoreDataViewController alloc] init];
    moreDataVC.navigationController = self.navigationController;
    NSInteger row = button.tag - 1000;
    switch (row)
    {
        case 0:
            moreDataVC.type = CategoryTypeMovie;
            break;
        case 1:
            moreDataVC.type = CategoryTypeTV;
            break;
        case 2:
            moreDataVC.type = CategoryTypeZY;
            break;
        case 3:
            moreDataVC.type = CategoryTypeCarton;
            break;
        default:
            break;
    }
    [self.navigationController pushViewController:moreDataVC animated:YES];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


@end
